package org.jeecg.modules.carLedger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.carLedger.entity.CarLedger;
import org.jeecg.modules.carLedger.mapper.CarLedgerMapper;
import org.jeecg.modules.carLedger.service.ICarLedgerService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 进出厂车辆电子台账
 * @Author: jeecg-boot
 * @Date:   2024-07-23
 * @Version: V1.0
 */
@Service
public class CarLedgerServiceImpl extends ServiceImpl<CarLedgerMapper, CarLedger> implements ICarLedgerService {

    @Override
    public CarLedger getLastByPlateNo(String plateNo) {
        LambdaQueryWrapper<CarLedger> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CarLedger::getPlateNo, plateNo);
        wrapper.isNull(CarLedger::getExitTime);
        wrapper.isNull(CarLedger::getExitPic);
        wrapper.orderByDesc(CarLedger::getCreateTime);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<Map<String, Object>> getFleetCharts(String start, String end) {
        // 查询数量最多的三个车队
        List<Map<String, Object>> maps = baseMapper.getTopThreeFleets(start, end);
        LambdaQueryWrapper<CarLedger> wrapper = Wrappers.lambdaQuery();
        wrapper.notIn(CarLedger::getFleet, maps.stream()
                .map(map -> map.get("fleet"))
                .collect(Collectors.toList()));
        wrapper.between(CarLedger::getCreateTime, start, end);
        Integer i = baseMapper.selectCount(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("fleet", "其他");
        map.put("count", i);
        maps.add(map);
        return maps;
    }

    @Override
    public List<Map<String, Object>> getGoodCharts(String start, String end) {
        List<Map<String, Object>> topThreeGoods = baseMapper.getTopThreeGoods(start, end);
        LambdaQueryWrapper<CarLedger> wrapper = Wrappers.lambdaQuery();
        wrapper.notIn(CarLedger::getGoods, topThreeGoods.stream()
                .map(map -> map.get("goods"))
                .collect(Collectors.toList()));
        wrapper.between(CarLedger::getCreateTime, start, end);
        Integer i = baseMapper.selectCount(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", "其他");
        map.put("count", i);
        topThreeGoods.add(map);
        return topThreeGoods;
    }

    @Override
    public int getTodayCount() {
        return baseMapper.getTodayCount();
    }

    @Override
    public int getVehiclePassRecordsNum() {
        return baseMapper.getVehiclePassRecordsNum();
    }

    @Override
    public int getMonthCount() {
        return baseMapper.getMonthCount();
    }

    @Override
    public List<Map<String, Object>> getFleetChartsLeading(String start, String end) {
        List<Map<String, Object>> maps = baseMapper.getTopThreeFleetsLeading(start, end);
        LambdaQueryWrapper<CarLedger> wrapper = Wrappers.lambdaQuery();
        wrapper.notIn(CarLedger::getFleet, maps.stream()
                .map(map -> map.get("fleet"))
                .collect(Collectors.toList()));
        wrapper.between(CarLedger::getCreateTime, start, end);
        wrapper.eq(CarLedger::getLeadingEnterprise, 1);
        Integer i = baseMapper.selectCount(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("fleet", "其他");
        map.put("count", i);
        maps.add(map);
        return maps;
    }

    @Override
    public List<Map<String, Object>> getGoodChartsLeading(String start, String end) {
        List<Map<String, Object>> topThreeGoods = baseMapper.getTopThreeGoodsLeading(start, end);
        LambdaQueryWrapper<CarLedger> wrapper = Wrappers.lambdaQuery();
        wrapper.notIn(CarLedger::getGoods, topThreeGoods.stream()
                .map(map -> map.get("goods"))
                .collect(Collectors.toList()));
        wrapper.between(CarLedger::getCreateTime, start, end);
        wrapper.eq(CarLedger::getLeadingEnterprise, 1);
        Integer i = baseMapper.selectCount(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", "其他");
        map.put("count", i);
        topThreeGoods.add(map);
        return topThreeGoods;
    }

}
